/**
 * 
 */
package com.changil.nowonchangilchurch.ui.item;

import uk.co.senab.photoview.PhotoView;
import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.widget.RelativeLayout;

import com.changil.nowonchangilchurch.R;
import com.changil.nowonchangilchurch.util.UrlUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * @author 홍창우
 *
 */
public class ScalableImageItem extends RelativeLayout {
	private Context mContext =null;
	private PhotoView mImage = null;
	private UserActionListener mUserActionListener = null;
	public interface UserActionListener {
		public void onItemTouched();
	}
	GestureDetector mGestureDetector = null;
	/**
	 * @param context
	 */
	public ScalableImageItem(Context context) {
		super(context);
		init(context);
	}

	/**
	 * @param context
	 * @param attrs
	 */
	public ScalableImageItem(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	/**
	 * @param context
	 * @param attrs
	 * @param defStyle
	 */
	public ScalableImageItem(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}
	
	private void init(Context context) {
		mContext = context;
		this.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		this.setBackgroundColor(0xFF000000);
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.scalable_image_item, this, true);
		mImage = (PhotoView)findViewById(R.id.scaleable_image_item_image);
		mGestureDetector = new GestureDetector(mContext, mSimpleOnGestureListener);
	}
	
	public void setImage(ImageLoader imageLoader, String url, DisplayImageOptions options) {
		if(null != url) {
			imageLoader.displayImage(UrlUtil.getThumbnailUrl(url, mContext.getResources().getDisplayMetrics().widthPixels), mImage, options, null);
		}
	}
	
	public void setUserActionListener(UserActionListener l) {
		mUserActionListener = l;
	}
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		mGestureDetector.onTouchEvent(ev);
		return super.dispatchTouchEvent(ev);
	}
	
	private SimpleOnGestureListener mSimpleOnGestureListener = new SimpleOnGestureListener() {

		@Override
		public boolean onDoubleTap(MotionEvent e) {
			return super.onDoubleTap(e);
		}

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			return super.onFling(e1, e2, velocityX, velocityY);
		}

		@Override
		public void onLongPress(MotionEvent e) {
			super.onLongPress(e);
		}

		@Override
		public boolean onSingleTapConfirmed(MotionEvent e) {
			if(null != mUserActionListener) {
				mUserActionListener.onItemTouched();
			}
			return super.onSingleTapConfirmed(e);
		}

	};
	
	
}
